/**
 * @project_name extjs4
 * @package_name com.gxwz.extjs.action
 * @file_name DeptManageAction.java
 */
package com.gxwz.extjs.action;

import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsDateJsonBeanProcessor;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.struts2.ServletActionContext;

import com.gxwz.extjs.model.Dept;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author chendequan
 * @email: 1527254027@qq.com
 * @Time: 2013-9-8 下午02:55:02
 * 
 */
@SuppressWarnings("serial")
public class DeptManageAction extends ActionSupport {

	private HttpServletResponse response = ServletActionContext.getResponse();
	private HttpServletRequest request = ServletActionContext.getRequest();

	/**
	 * 跟extjs的store层的类相关联 把数据用json的格式传送到js文件里。extjs会帮我们自动解析数据
	 * 
	 * @datetime 2013-9-8 下午04:03:24
	 * @throws IOException
	 */
	public void getDept() throws IOException {
		Dept d01 = new Dept();
		d01.setId("01");
		d01.setText("技术部");

		Dept d1 = new Dept();
		d1.setId("0101");
		d1.setText("研发部");
		d1.setLeaf(true);

		Dept d2 = new Dept();
		d2.setId("0102");
		d2.setText("实施部");
		d2.setLeaf(true);

		Set<Dept> ds1 = new HashSet<Dept>();
		ds1.add(d1);
		ds1.add(d2);
		d01.setChildren(ds1);
		// ==--------------------------
		Dept d02 = new Dept();
		d02.setId("02");
		d02.setText("后勤部");

		Dept d0201 = new Dept();
		d0201.setId("0201");
		d0201.setText("人事部");
		d0201.setLeaf(true);

		Dept d0202 = new Dept();
		d0201.setId("0202");
		d0202.setText("采购部");
		d0202.setLeaf(true);

		Set<Dept> ds2 = new HashSet<Dept>();
		ds2.add(d0201);
		ds2.add(d0202);
		d02.setChildren(ds2);

		Dept root = new Dept();
		root.setText("root");
		root.setId("0");
		Set<Dept> rs = new HashSet<Dept>();
		rs.add(d01);
		rs.add(d02);
		root.setChildren(rs);

		// JsonConfig cfg = FormatDate4Json.getCfgByYYYYMMDD();
		JsonConfig cfg = new JsonConfig();
		// 当输出时间格式时，采用和JS兼容的格式输出
		cfg.registerJsonBeanProcessor(Date.class, new JsDateJsonBeanProcessor());
		cfg.setIgnoreDefaultExcludes(false);
		cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		JSONObject jsonObject = JSONObject.fromObject(root, cfg);

		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		writer.write(jsonObject.toString());
		writer.close();
	}

	/**
	 * 读取部门记录设置到表格中
	 * @datetime 2013-9-13 下午02:10:40
	 * @throws IOException
	 */
	public void readDeptRecord4GridTree() throws IOException {
		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		StringBuffer sb = new StringBuffer("{\"total\":\"" + 0
				+ "\", \"topics\":[");
		// 还未有任何数据，一空json对象
		sb.append("]}");
		writer.write(sb.toString());
		writer.close();
	}

	/**
	 * 读取树形结构的的数据
	 * @datetime 2013-9-13 下午02:11:07
	 * @throws IOException
	 */
	public void readDeptTree() throws IOException {
		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		StringBuffer sb = new StringBuffer(
				"{\"id\":\"-1\", \"leaf\":false,\"text\":\"root\", children:[]}");
		// 还未有任何数据，一空json对象
		writer.write(sb.toString());
		writer.close();
	}
	
	/**
	 * 更新部门列表信息
	 * @datetime 2013-9-13 下午02:12:52
	 * @throws IOException
	 */
	public void updateDeptList() throws IOException {
		/*
		 * request.getParameter("data") 接收从extjs页面传过来的信息
		 * GridActionUtil.js文件29行------
		 * params: {data: "["+data.join(',')+"]"},
		 */
		System.out.println(request.getParameter("data"));
		response.setCharacterEncoding("utf-8");
		Writer writer = response.getWriter();
		
		/**
		 * 处理和页面交互的ajax后把处理的结果放到Writer流里响应到extjs页面
		 */
//		响应到页面的信息，不需要像struts2那样配置跳转的信息
		StringBuffer sb = new StringBuffer(
				"{success: 'update ok'}");
		// 还未有任何数据，一空json对象
		writer.write(sb.toString());
		writer.close();
	}
}
